package com.gomtel.smartdevice.widget;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by Administrator on 2018/5/22 0022.
 */

public class CalenderUtilsClass {

    private static List<String> printWeekdays;

    /**
     * 根据当前日期获取本周日历
     */
    public String getTimeInterval(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("y-M-d", Locale.CHINA);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        String imptimeBegin = sdf.format(cal.getTime());
        // System.out.println("所在周星期一的日期：" + imptimeBegin);
        cal.add(Calendar.DATE, 6);
        String imptimeEnd = sdf.format(cal.getTime());
        // System.out.println("所在周星期日的日期：" + imptimeEnd);
        return imptimeBegin + "/" + imptimeEnd;
    }

    /**
     * 根据当前日期获得上周的日期区间（上周周一和周日日期）
     *
     * @return
     * @author zhaoxuepu
     */
    public String getLastTimeInterval(int index) {
        SimpleDateFormat sdf = new SimpleDateFormat("y-M-d", Locale.CHINA);
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
        int offset1 = 1 - dayOfWeek;
        int offset2 = 7 - dayOfWeek;
        calendar1.add(Calendar.DATE, offset1 - 7 * index);
        calendar2.add(Calendar.DATE, offset2 - 7 * index);
        // System.out.println(sdf.format(calendar1.getTime()));// last Monday
        String lastBeginDate = sdf.format(calendar1.getTime());
        // System.out.println(sdf.format(calendar2.getTime()));// last Sunday
        String lastEndDate = sdf.format(calendar2.getTime());
        return lastBeginDate + "/" + lastEndDate;
    }

    /**
     * 获取一周开始到结束的list集合
     *
     * @return
     * @author zhaoxuepu
     */
    public static List<String> printWeekdays() {
        printWeekdays = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        setToFirstDay(calendar);
        for (int i = 0; i < 7; i++) {
            printDay(calendar);
            calendar.add(Calendar.DATE, 1);
        }
        return printWeekdays;
    }

    public static void setToFirstDay(Calendar calendar) {
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DATE, -1);
        }
    }

    public static void printDay(Calendar calendar) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("y-M-d");
        printWeekdays.add(dateFormat.format(calendar.getTime()));
    }

    /**
     * 获取指定一周开始到结束的list集合
     *
     * @return
     * @author zhaoxuepu
     */
    public static List<String> getLastWeek(int index) {
        List<String> printWeekdays = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        int n = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (n == 0) {
            n = 7;
        }
        cal.add(Calendar.DATE, -(index * 7 + (n - 1)));// 上周一的日期
        SimpleDateFormat sdf = new SimpleDateFormat("d", Locale.CHINA);
        Date date = cal.getTime();
        String monday = sdf.format(date);
        printWeekdays.add(monday);
        for (int i = 0; i < 6; i++) {
            cal.add(Calendar.DATE, 1);
            Date currentDay = cal.getTime();
            printWeekdays.add(sdf.format(currentDay));
        }
        return printWeekdays;
    }


    /**
     * 获取指定月开始到结束的list集合
     *
     * @return
     * @author zhaoxuepu
     */
    public static List<String> getDayListOfMonth(int index) {
        List<String> list = new ArrayList();
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.add(Calendar.MONTH, index);
        int year = aCalendar.get(Calendar.YEAR);//年份
        int month = aCalendar.get(Calendar.MONTH) + 1;//月份
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String MonthDay = String.valueOf(year) + "-" + month + "-" + i;
            list.add(MonthDay);
//            LogUtil.e("getDayListOfMonth", "MonthDay : " + MonthDay);
        }
        return list;
    }


    /**
     * 获取当前时间的前一天时间
     *
     * @param cl
     * @return
     */
    public static String getBeforeDay(Calendar cl) {
        //使用roll方法进行向前回滚
        //cl.roll(Calendar.DATE, -1);
        //使用set方法直接进行设置
        int currentDay = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, currentDay - 1);
        int year = cl.get(Calendar.YEAR);
        int month = cl.get(Calendar.MONTH) + 1;
        int day = cl.get(Calendar.DATE);
        return String.valueOf(year) + "-" + month + "-" + day;
    }

    /**
     * 获取当前时间的后一天时间
     *
     * @param cl
     * @return
     */
    public static String getAfterDay(Calendar cl) {
        //使用roll方法进行回滚到后一天的时间
//        cl.roll(Calendar.DATE, 1);
        //使用set方法直接设置时间值
        int currentDay = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, currentDay + 1);
        int year = cl.get(Calendar.YEAR);
        int month = cl.get(Calendar.MONTH) + 1;
        int day = cl.get(Calendar.DATE);
        return String.valueOf(year) + "-" + month + "-" + day;
    }

    /**
     * 获取当前日期
     *
     * @param
     * @return
     */
    public static String getCurrentDay(boolean status) {
        String currentTime;
        Calendar cl = Calendar.getInstance();
        int year = cl.get(Calendar.YEAR);
        String month = (cl.get(Calendar.MONTH) + 1) >= 10 ? ("" + (cl.get(Calendar.MONTH) + 1)) : ("0" + (cl.get(Calendar.MONTH) + 1));
        String day = cl.get(Calendar.DATE) >= 10 ? ("" + cl.get(Calendar.DATE)) : ("0" + cl.get(Calendar.DATE));
        String hour = (cl.get(Calendar.HOUR_OF_DAY) >= 10) ? (cl.get(Calendar.HOUR_OF_DAY) + "") : ("0" + cl.get(Calendar.HOUR_OF_DAY));
        String min = (cl.get(Calendar.MINUTE) >= 10 ? (cl.get(Calendar.MINUTE) + "") : ("0" + cl.get(Calendar.MINUTE)));
        String sec = (cl.get(Calendar.SECOND) >= 10) ? ("" + cl.get(Calendar.SECOND)) : ("0" + cl.get(Calendar.SECOND));
        currentTime = String.valueOf(year) + "-" + month + "-" + day;
        if (status) {
            currentTime = String.valueOf(year) + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
        }
        return currentTime;
    }

    /**
     * 获取当前小时
     *
     * @param
     * @return
     */
    public static int getCurrentHour() {
        Calendar cl = Calendar.getInstance();
        int hour = cl.get(Calendar.HOUR_OF_DAY);
        return hour;
    }

    /**
     * 取得当月天数
     */
    public static int getCurrentMonthLastDay() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 得到指定月的天数
     */
    public static int getMonthLastDay(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 获取指定日期(月)
     */
    public static String getAssignDay(int index) {
        String currentTime;
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.add(Calendar.MONTH, index);
        int year = aCalendar.get(Calendar.YEAR);//年份
        int month = aCalendar.get(Calendar.MONTH) + 1;//月份
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        currentTime = new StringBuffer().append(month).append("月").toString();
        return currentTime;
    }

    /**
     * 根据生日获取年龄
     */
    public static int getAgeByBirthDay(String birthDay) {
        if (birthDay == null || birthDay.length() < 4) {
            return 0;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //得到当前的年份
        String cYear = sdf.format(new Date()).substring(0, 4);
        String cMouth = sdf.format(new Date()).substring(5, 7);
        String cDay = sdf.format(new Date()).substring(8, 10);
        //得到生日年份
        String birth_Year = birthDay.substring(0, 4);
        String birth_Mouth = birthDay.substring(5, 7);
        String birth_Day = birthDay.substring(8, 10);
        int age = Integer.parseInt(cYear) - Integer.parseInt(birth_Year);
        if ((Integer.parseInt(cMouth) - Integer.parseInt(birth_Mouth)) < 0) {
            age = age - 1;
        } else if ((Integer.parseInt(cMouth) - Integer.parseInt(birth_Mouth)) == 0) {
            if ((Integer.parseInt(cDay) - Integer.parseInt(birth_Day)) > 0) {
                age = age - 1;
            } else {
                age = Integer.parseInt(cYear) - Integer.parseInt(birth_Year);
            }
        } else if ((Integer.parseInt(cMouth) - Integer.parseInt(birth_Mouth)) > 0) {
            age = Integer.parseInt(cYear) - Integer.parseInt(birth_Year);
        }
        return age;
    }

    //获取当前日期前30天集合
    public static List<String> getIntervalDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
        ArrayList<String> list = new ArrayList<>();
        try {
            Calendar calc = Calendar.getInstance();
            for (int i = 0; i < 30; i++) {
                calc.setTime(new Date());
                calc.add(calc.DATE, -(30 - i));
                Date minDate = calc.getTime();
                list.add(sdf.format(minDate));
            }

            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
